package com.woniuxy.enjoylife.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.enjoylife.entity.Server;
import com.woniuxy.enjoylife.entity.dto.ReturnServerDto;
import com.woniuxy.enjoylife.entity.dto.SelectServerDto;
import com.woniuxy.enjoylife.service.ServerService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.PageUtil;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author teamEnjoyLife75
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/server")
public class ServerController {
    @Resource
    ServerService serverService;
    @Resource
    ResponseResult responseResult;
    @Autowired
    Server server;
    @Autowired
    ServerController serverController;
    @Resource
    private HttpServletRequest request;

    /**
     * 文件上传之图片显示
     * @param name
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/load/{name}")
    public void load(@PathVariable("name") String name, HttpServletResponse response) throws Exception {
        String path = "D:\\demo\\" + name;
        File file = new File(path);

        response.setDateHeader("Expires", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("imge/jpeg");

        //
        BufferedImage image = ImageIO.read(file);
        ImageIO.write(image, "png", response.getOutputStream());
    }

    /**
     * 分页展示商家服务
     */
    @ResponseBody
    @GetMapping("findByPage/{size}/{currentPage}")
    public ResponseResult<Server> page(@PathVariable Integer size, @PathVariable Integer currentPage) {

//        System.out.println("当前页：" + currentPage + "显示条数：" + size);
        int uid =(int) request.getSession().getAttribute("id");

        QueryWrapper<Server> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid );

        Page page = serverService.page(new Page<>(currentPage, size),wrapper);
        PageResult pageResult = PageUtil.setPageResult(page);
        pageResult.setData(page.getRecords());


        responseResult.setData(pageResult);
        responseResult.success();

        return responseResult;
    }

    /**
     * 商家增加服务项目
     */
    @RequestMapping("save")
    @ResponseBody
    public ResponseResult<Server> save(Server server) {

        int uid =(int) request.getSession().getAttribute("id");
        server.setUid(uid);
        boolean save = serverService.save(server);

        return save?responseResult.success():responseResult.error();
    }

    /**
     * 修改商家服务
     */
//@PostMapping("update/{params}")
//@PostMapping("updateById")
    @RequestMapping("updateById")
    @ResponseBody
    public ResponseResult updataById(Server server) {
        System.out.println("修改服务" + server);
        serverService.saveOrUpdate(server);
        return responseResult;
    }

    /**
     * 删除商家服务
     */

    @DeleteMapping("deleteOne/{id}")
    @ResponseBody
    public ResponseResult deleteOne(@PathVariable("id") int id) {
        System.out.println("接收" + id);
        boolean result = serverService.updateById(new Server().setId(id).setStatus(1));
        if (result) {
            ResponseResult responseResult = this.responseResult.success();
        } else {
            ResponseResult responseResult = this.responseResult.error();
        }
        return responseResult;
    }

    @GetMapping("findByName")
    @ResponseBody
    public ResponseResult findByName(String name) {
        System.out.println("获取字段" + name);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name",name );
        Page<Server> page = new Page<>(1, 5);
        Page page1 = serverService.page(page, wrapper);
        responseResult.setData(page1);
        return responseResult;
    }
    //排序
    @GetMapping("sorting/{sortCol}/{rule}/{size}/{currentPage}")
    public  ResponseResult<List<Server>> sorting(@PathVariable("currentPage") Integer currentPage,@PathVariable("size") Integer size,@PathVariable("sortCol")  String sortCol,@PathVariable("rule") String rule){
        com.github.pagehelper.Page<Object> page = PageHelper.startPage(currentPage, size);
        List<Server> serverList = serverService.sorting(sortCol, rule);
        PageResult pageResult = new PageResult();
        pageResult.setData(serverList);
        pageResult.setTotal(page.getTotal());
        pageResult.setPageSize(size);
        pageResult.setTotalPage(page.getPages());
        pageResult.setCurrentPage(currentPage);
        responseResult.setData(pageResult);
        return responseResult;
    }
//    查询详情
    @GetMapping("findServerById/{id}")
public ResponseResult findServerById(@PathVariable("id") Integer id){

        Server server = serverService.getById(id);
        responseResult.setData(server);
        responseResult.success();
        return responseResult;
}
    //用户块---------------------------------------------------------

    //获取服务列表（分类id/当前页/每页大小/服务名称/服务内容）
    @RequestMapping("/userSelectServer")
    public PageResult<ReturnServerDto> userSelectServer(SelectServerDto ssd) {
        ssd.setUid((int) request.getSession().getAttribute("id"));
        return serverService.userSelectServer(ssd);
    }


    //获取服务列表（分类id/当前页/每页大小/服务名称/服务内容）(平台用)
    @RequestMapping("/selectServer")
    public PageResult<ReturnServerDto> selectServer(SelectServerDto ssd) {
        return serverService.selectServer(ssd);
    }



}

